﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GitTool
{
    public class LogTool
    {

        /// <summary>
        /// 定义委托接口处理函数，用于实时处理cmd输出信息
        /// </summary>
        public delegate void Callback(String line);

        delegate void OutPut_Call(String line, RichTextBox textBox, Control root);


        # region 通用信息输出逻辑

        /// <summary>
        /// 此函数用于实时显示cmd输出信息, textBox为输出信息显示控件，root为textBox的根
        /// </summary>
        public static void OutPut(String line, RichTextBox textBox, Control root)
        {
            if (textBox.InvokeRequired)
            {
                OutPut_Call F = new OutPut_Call(OutPut);
                root.Invoke(F, new object[] { line, textBox, root });
            }
            else
            {
                if (line.StartsWith("jarfileName:")) return;     // 不输出签名信息

                // 超出RichTextBox最大容量时，自动清除
                if (textBox.TextLength + line.Length + 10 > textBox.MaxLength)
                    textBox.Text = "";

                line = replace(line);
                textBox.AppendText(Environment.NewLine);
                textBox.SelectionColor = getColor(line);
                textBox.AppendText(line);

                textBox.ScrollToCaret();
                textBox.Invalidate();

                //textBox1.Text += line + "\r\n";
            }
        }

        /// <summary>
        /// 替换输出信息的显示格式
        /// </summary>
        private static String replace(String str)
        {
            if (str.StartsWith("I:")) str = str.Replace("I:", "【I】");
            else if (str.StartsWith("jarfileName:")) str = "【I】" + str;
            return str;
        }

        /// <summary>
        /// 根据输出信息，设置显示颜色
        /// </summary>
        private static Color getColor(String str)
        {
            if (str.StartsWith("cmd>")) return Color.DarkBlue;
            else if (str.StartsWith("【I】")) return Color.Green;
            else if (str.StartsWith("【I2】")) return Color.RoyalBlue;
            else if (str.StartsWith("【I3】")) return Color.FromArgb(91, 186, 244);
            else if (str.StartsWith("【E】")) return Color.Red;
            else if (str.StartsWith("【E2】")) return Color.BlueViolet;
            else if (str.StartsWith("【E3】")) return Color.Purple;
            else if (str.StartsWith("【V】")) return Color.DarkSlateGray;
            else if (str.StartsWith("【L】")) return Color.FromArgb(57, 225, 126);
            else if (str.StartsWith("【T】")) return Color.FromArgb(102, 0, 51);
            else return Color.Black;
        }

        # endregion
    }
}
